﻿#include "SizeEditEntry.h"
#include "IntegerEditEntry.h"

struct SizeEditEntry::SizeEditEntryPrivate {
    IntegerEditEntry *entryWidth{nullptr};
    IntegerEditEntry *entryHeight{nullptr};
};

SizeEditEntry::~SizeEditEntry() { }

SizeEditEntry::SizeEditEntry(QWidget *parent) : StringEditEntry{parent} {
    p.reset(new SizeEditEntryPrivate);
    initUi();
    retranslateUi();
}

void SizeEditEntry::setValue(const QSize &size) {
    const auto &text = QString("%1x%2").arg(size.width()).arg(size.height());
    StringEditEntry::setValue(text);
    p->entryWidth->setValue(size.width());
    p->entryHeight->setValue(size.height());
}

QSize SizeEditEntry::value() const {
    QSize size;
    size.setWidth(p->entryWidth->value());
    size.setHeight(p->entryHeight->value());
    return size;
}

void SizeEditEntry::onChildEntryValueChanged() {
    emit valueChanged(value());
}

void SizeEditEntry::initUi() {
    p->entryWidth = new IntegerEditEntry;
    appendChildEntry(p->entryWidth);
    p->entryWidth->setRange(0, QWIDGETSIZE_MAX);
    connect(p->entryWidth, &IntegerEditEntry::valueChanged, this, &SizeEditEntry::onChildEntryValueChanged);

    p->entryHeight = new IntegerEditEntry;
    appendChildEntry(p->entryHeight);
    p->entryHeight->setRange(0, QWIDGETSIZE_MAX);
    connect(p->entryHeight, &IntegerEditEntry::valueChanged, this, &SizeEditEntry::onChildEntryValueChanged);
}

void SizeEditEntry::retranslateUi() {
    p->entryWidth->setTitle(tr("width"));
    p->entryHeight->setTitle(tr("height"));
}
